package com.jiang.android.cprogressbutton.slice;

import com.jiang.android.cprogressbutton.ResourceTable;
import com.jiang.android.pbutton.CProgressButton;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        CProgressButton.initStatusString(new String[]{"download", "pause", "complete", "error", "delete"});

        button1();
        button2();
        button3();
    }

    private void button3() {
        final CProgressButton progressButton = (CProgressButton) findComponentById(ResourceTable.Id_btn3);


        final Text tv = (Text) findComponentById(ResourceTable.Id_state3);
        progressButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                AnimatorValue animatorValue = new AnimatorValue();
                if (progressButton.getState() == CProgressButton.STATE.NORMAL) {
                    progressButton.startDownLoad();
                    animatorValue.setDuration(5000);
                    animatorValue.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
                        @Override
                        public void onUpdate(AnimatorValue animatorValue, float v) {
                            int value = (int) (100 * v);
                            tv.setText("state progress:" + value);
                            if (value == 100) {
                                progressButton.normal(2);
                                tv.setText("state normal");
                            }
                            progressButton.download(value);
                        }

                    });
                    animatorValue.start();
                } else {
                    animatorValue.cancel();
                    progressButton.normal(0);
                    tv.setText("state normal");
                }

            }
        });
    }

    private void button2() {
        final CProgressButton progressButton = (CProgressButton) findComponentById(ResourceTable.Id_btn2);


        final Text tv = (Text) findComponentById(ResourceTable.Id_state2);
        progressButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                AnimatorValue animatorValue = new AnimatorValue();
                if (progressButton.getState() == CProgressButton.STATE.NORMAL) {
                    animatorValue.setDuration(5000);
                    animatorValue.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
                        @Override
                        public void onUpdate(AnimatorValue animatorValue, float v) {

                            int value = (int) (100 * v);
                            tv.setText("state progress:" + value);
                            if (value == 100) {
                                progressButton.normal(2, false);
                                tv.setText("state normal");
                            }
                            progressButton.download(value);
                        }

                    });
                    animatorValue.start();
                } else {
                    animatorValue.cancel();
                    progressButton.normal(0, false);
                    tv.setText("state normal");
                }

            }
        });
    }

    private void button1() {
        final CProgressButton progressButton = (CProgressButton) findComponentById(ResourceTable.Id_btn);

        final Text tv = (Text) findComponentById(ResourceTable.Id_state);
        progressButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                AnimatorValue animatorValue = new AnimatorValue();
                if (progressButton.getState() == CProgressButton.STATE.NORMAL) {
                    progressButton.startDownLoad();
                    animatorValue.setDuration(5000);
                    animatorValue.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
                        @Override
                        public void onUpdate(AnimatorValue animatorValue, float v) {
                            int value = (int) (100 * v);
                            tv.setText("state progress:" + value);
                            if (value == 100) {
                                progressButton.normal(2);
                                tv.setText("state normal");
                            }
                            progressButton.download(value);
                        }

                    });
                    animatorValue.start();
                } else {
                    animatorValue.cancel();
                    progressButton.normal(4);
                    tv.setText("state normal");
                }

            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
